import pygame

class SpeedMenu:
    def __init__(self, screen, config, game):
        self.screen = screen
        self.config = config
        self.game = game
        self.font = pygame.font.Font(self.config.font_path, 36)
        self.small_font = pygame.font.Font(self.config.font_path, 24)
        self.options = ["按[Esc]返回"]
        self.selected_option = 0
        self.speed_multiplier = 1.0  # 速度调节倍率

    def draw(self):
        self.screen.fill(self.config.colors['black'])
        # 显示当前小球速度
        ball_speed_text = self.font.render(
            f"小球速度: {self.game.ball.velocity.length():.2f}", True, self.config.colors['white'])
        self.screen.blit(ball_speed_text, (self.config.screen_width // 2 - 150, 100))

        # 如果是鸟群算法关卡，显示砖块速度
        if self.game.brick_wall.level_type == "flocking":
            brick_speed_text = self.font.render(
                f"砖块速度: {self.game.brick_wall.bricks.sprites()[0].velocity.length():.2f}",
                True, self.config.colors['white'])
            self.screen.blit(brick_speed_text, (self.config.screen_width // 2 - 150, 150))
        else:
            # 如果不是鸟群算法关卡，显示提示信息
            self.draw_warning_message()

        # 绘制返回按钮
        for i, option in enumerate(self.options):
            color = self.config.colors['white'] if i != self.selected_option else self.config.colors['red']
            option_text = self.font.render(option, True, color)
            self.screen.blit(option_text, (self.config.screen_width // 2 - 100, 300 + i * 60))
        pygame.display.flip()

    def draw_warning_message(self):
        """
        在屏幕上绘制警告文本，并将其包裹在一个矩形内。
        """
        # 创建警告文本
        warning_text = self.small_font.render(
            "当前关卡不支持调节砖块速度！", True, self.config.colors['red']
        )

        # 获取文本的尺寸
        text_width, text_height = warning_text.get_size()

        # 定义矩形的尺寸和位置
        padding = 20  # 内边距
        rect_width = text_width + 2 * padding
        rect_height = text_height + 2 * padding
        rect_x = self.config.screen_width // 2 - rect_width // 2  # 矩形水平居中
        rect_y = 150 - padding  # 垂直位置调整为包裹文本

        # 绘制矩形背景
        pygame.draw.rect(
            self.screen,
            self.config.colors['white'],  # 背景颜色
            (rect_x, rect_y, rect_width, rect_height),
            border_radius=10  # 圆角矩形
        )

        # 绘制文本
        text_x = rect_x + padding
        text_y = rect_y + padding
        self.screen.blit(warning_text, (text_x, text_y))


    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    self._adjust_speed(1.1)  # 增加速度
                elif event.key == pygame.K_DOWN:
                    self._adjust_speed(0.9)  # 减少速度
                elif event.key == pygame.K_RETURN or event.key == pygame.K_ESCAPE:
                    return 0  # 返回设置界面
        return -1

    def _adjust_speed(self, multiplier):
        """调节小球和砖块速度"""
        self.speed_multiplier *= multiplier
        # 调整小球速度
        self.game.ball.velocity.scale_to_length(self.game.ball.velocity.length() * multiplier)
        # 如果是鸟群算法关卡，调整砖块速度
        if self.game.brick_wall.level_type == "flocking":
            for brick in self.game.brick_wall.bricks:
                brick.velocity.scale_to_length(brick.velocity.length() * multiplier)
